﻿using System;
using System.Linq; 

namespace Helpers.Validation
{
    /// <summary>
    /// Contains a set of validation helpers methods.
    /// </summary>
    public static class Helper
    {
        #region Static Methods

        public static string ValidateNumber(object value, double min, double max, bool allowDecimals = true)
        {
            double number = 0;

            bool validNumber = Double.TryParse(value.ToString(), out number);
            if (!validNumber) return "Invalid number.";

            if (!allowDecimals)
            {
                if (number % 1 != 0) return "Invalid number: the value can't have decimal places.";
            }

            if ((number < min) || (number > max))
            {
                return "Please enter a number in the range: " + min + " - " + max + ".";
            }

            return string.Empty;
        }

        public static bool AllDifferent(params object[] args)
        {
            string[] ignore = {};

            if (args[args.Length - 1] is string[])
            {
                ignore = (string[]) args[args.Length - 1];
                args = args.Where(p => ((p is string[]) == false)).ToArray(); 
            }

            bool valid = true;
            int inx = 0; 
            foreach (object ob in args)
            {
                inx++;
                for (int x = inx; x < args.Length; x++)
                {
                    if (ignore.Contains(ob.ToString()) == false)
                    {
                        if (ob.ToString() == args[x].ToString()) { valid = false; break; }
                    }
                }
            }
            return valid; 
        }

        #endregion
    }
}
